//
//  MYGAcademicSchuledDetailCell.swift
//  MYDoctor
//
//  Created by 小火科技 on 2024/5/30.
//

import UIKit

class MYGAcademicSchuledDetailCell: UITableViewCell,UITextViewDelegate {
    
    var clickNameBlock:((_ id:Int)->Void)?
    
//    private var names:[String] = []
    private var personIds:[Int] = []
    var model:MYGAcademicForeignCellModel?{
        didSet {
            guard let item = model else {return}
         
            var names:[String] = []
            item.models.forEach { obj in
                names.append(obj.expertCn)
                personIds.append(obj.id)
            }
            
            if item.isSpeechCn {
//                nameLabel.text = name.replacingOccurrences(of: ",", with: "\n")
                var name = names.joined(separator: ",")
                name = name.replacingOccurrences(of: ",", with: "\n")
                let attributedString = NSMutableAttributedString(string: name)
                let paragraphStyle = NSMutableParagraphStyle()
                paragraphStyle.lineSpacing = 5 // 设置行间距
                let text = name
                var tag = 1 // 用于区分不同名字的唯一标识符
                for name in names {
                    let range = (text as NSString).range(of: name)
                    if range.location != NSNotFound {
                        attributedString.addAttribute(.link, value: "\(tag)", range: range) // 使用tag作为链接值
                   
                        attributedString.addAttribute(.paragraphStyle, value: paragraphStyle, range: NSMakeRange(0, attributedString.length))
//                        attributedString.addAttribute(.underlineStyle, value: NSUnderlineStyle.single.rawValue, range: range) // 可选：添加下划线样式
                        tag += 1
                    }
                }
                nameTextView.attributedText = attributedString
                contentLabel.text = item.title
            }else {
                let subName = names.joined(separator: ",")
                if !subName.isEmpty {
                    contentLabel.text = "\(item.title):\(subName)"
                }
            }
            timeLabel.text = item.time
            mettingLabel.text = item.role
            
        }
        
    }
    
    func textView(_ textView: UITextView, shouldInteractWith URL: URL, in characterRange: NSRange, interaction: UITextItemInteraction) -> Bool {
        let link = URL.absoluteString
        if let tag = Int(link) {
//            print("点击了名字: \(names[tag-1])")
            if tag <= personIds.count {
                clickNameBlock?(personIds[tag-1])
            }
            return false // 阻止系统默认的URL处理行为
        }
        return true
    }
    
    // MARK: - init
    override init(style: UITableViewCell.CellStyle, reuseIdentifier: String?) {
        super.init(style: style, reuseIdentifier: reuseIdentifier)
        selectionStyle = .none
        contentView.backgroundColor = .white
        configUI()
    }
    
    required init?(coder: NSCoder) {
        fatalError("init(coder:) has not been implemented")
    }

    // MARK: - UI
    private func configUI() {
        contentView.backgroundColor = .white
        
        contentView.addSubview(timeLabel)
        contentView.addSubview(contentLabel)
        contentView.addSubview(mettingLabel)
//        contentView.addSubview(nameLabel)
        contentView.addSubview(nameTextView)
        contentView.addSubview(line)
        
        timeLabel.snp.makeConstraints { make in
            make.left.equalToSuperview()
            make.top.equalToSuperview().offset(15.5)
            make.width.equalTo(100)
//            make.height.equalTo(20)
        }
        
//        nameLabel.snp.makeConstraints { make in
//            make.top.equalTo(timeLabel)
//            make.width.equalTo(70)
//            make.right.equalToSuperview()
//        }
        
        nameTextView.snp.makeConstraints { make in
            make.top.equalTo(timeLabel)
            make.width.equalTo(70)
            make.right.bottom.equalToSuperview()
//            make.bottom.equalToSuperview()
        }
        
        mettingLabel.snp.makeConstraints { make in
            make.top.equalTo(timeLabel)
            make.width.equalTo(70)
            make.right.equalTo(nameTextView.snp.left)
        }
        
        contentLabel.snp.makeConstraints { make in
            make.left.equalTo(timeLabel.snp.right)
            make.right.equalTo(mettingLabel.snp.left)
            make.top.equalTo(timeLabel)
        }

        line.snp.makeConstraints { make in
            make.height.equalTo(0.5)
            make.left.bottom.right.equalToSuperview()
        }
        
    }
    private lazy var timeLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(97, 97, 97, 1)
        label.textAlignment = .center
        return label
    }()
    private lazy var contentLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(97, 97, 97, 1)
        label.numberOfLines = 0
//        label.backgroundColor = .orange
        return label
    }()

    private lazy var mettingLabel: UILabel = {
        let label = UILabel()
        label.font = .regular(12)
        label.textColor = .rgba(97, 97, 97, 1)
        label.textAlignment = .center
        return label
    }()
    
    private lazy var nameLabel: UILabel = {
        let label = UILabel()
        label.font = .medium(12)
        label.textColor = .rgba(27, 79, 218, 1)
        label.textAlignment = .center
        label.numberOfLines = 0
        return label
    }()
    
    private lazy var nameTextView:UITextView = {
        let textView = UITextView()
       textView.isEditable = false
       textView.isSelectable = true
       textView.delegate = self
//        textView.backgroundColor = .red
        textView.textContainerInset = .zero
       return textView
        
    }()
    
    private lazy var line:UIView = {
        let line = UIView()
        line.backgroundColor = .rgba(238, 238, 238, 1)
        return line
    }()
    
}
